package Test;



public class 重建二叉树 {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return build(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
    }

    TreeNode build(int[] preorder,int start,int end,int[] inorder,int start1,int end1){
        if(start>end) return null;
        int pre=preorder[start];
        int index=0;
        for (int i=start1;i<=end1;i++){
            if(inorder[i]==pre) index=i; break;
        }
        int leftsize=index-start1;
        TreeNode rootnode=new TreeNode(pre);
        rootnode.left=build(preorder,start+1,start+leftsize,inorder,start1,index-1);
        rootnode.right=build(preorder,start+leftsize+1,end,inorder,index,end1);
        return rootnode;
    }

}
